package research.ui.editors;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.forms.widgets.Section;
import org.eclipse.ui.forms.widgets.TableWrapData;

import research.domain.Calander;
import research.domain.Parameter;
import research.entity.Entity;
import research.entity.EntityType;
import research.model.CalanderHierarchy;
import research.persistence.PersistenceManager;
import research.ui.editors.input.EntityEditorInput;
import research.ui.editors.tableeditors.calander.CalanderTableEditor;

public class CalanderEditor extends FormEntityEditor {
	public static String ID = "research.ui.editors.CalanderEditor";
	private Text name;
	private Text location;
	private Text description;
	private CalanderTableEditor valuesEditor;
	private List<Entity> values;
	private List<Entity> initValues;
	
	private Calander getEntity() {
		return (Calander) entity;
	}

	@Override
	protected boolean checkInputEntityType(EntityEditorInput input) {
		return input.getEntity().getType().equals(EntityType.Calander);
	}

	@Override
	protected void fill() {
		this.name.setText(this.getEntity().getName());
		this.location.setText(this.getEntity().getLocation());
		this.description.setText(this.getEntity().getDescription());
		
		this.valuesEditor.setInput(new ArrayList<Entity>(values));
		valuesEditor.addModifyListener(getEntityTableListener());
	}

	@Override
	protected void initialize() {
		if (!isNewEntity()) {
			values = PersistenceManager.getInstance().getDataSource().getChildren(getEntity(), new CalanderHierarchy());
		} else {
			values = new ArrayList<Entity>();
		}
		
		initValues = new ArrayList<Entity>(values);
	}
	
	@Override
	protected void persist() {
		
		for (Entity oldpar : initValues) {
			if (!valuesEditor.getEntityList().getEntities().contains(oldpar)) {
				getPersistenceManager().remove(oldpar);
			}
		}

		for (Entity par : valuesEditor.getEntityList().getEntities()) {
			Parameter val = (Parameter) par;
			val.setCalander(getEntity());
			getPersistenceManager().persist(val);
		}
	}

	@Override
	protected boolean readAndValidate() {
		this.getEntity().setName(this.name.getText());
		this.getEntity().setLocation(this.location.getText());
		this.getEntity().setDescription(this.description.getText());
		
		for (Entity par : valuesEditor.getEntityList().getEntities()) {
			Parameter val = (Parameter) par;
				if (val.getLow() > val.getHigh()) {
					MessageDialog.openWarning(getSite().getShell(), "Error", "Неверно задан диапазон допустимых значений параметра:\n" + val.getName());
					return false;
				}
		}

		return validateText(name);
	}

	@Override
	protected void renderForm() {
		TableWrapData td;
		Section section;
		Composite composite;
		GridData gd;
		
		/*
		 * Name
		 */
		section = toolkit.createSection(form.getBody(), Section.TITLE_BAR);
		td = new TableWrapData(TableWrapData.FILL);
		section.setLayoutData(td);
		section.setText("Название*");
		composite = toolkit.createComposite(section);
		composite.setLayout(new GridLayout(1, false));
		section.setClient(composite);
		gd = new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL);
		name = toolkit.createText(composite, "",
				SWT.BORDER);
		gd.widthHint = 100;
		name.setLayoutData(gd);
		name.addModifyListener(getNameModifyListener());
		
		/*
		 * Location
		 */
		section = toolkit.createSection(form.getBody(), Section.TITLE_BAR);
		td = new TableWrapData(TableWrapData.FILL);
		section.setLayoutData(td);
		section.setText("Место расположения");
		composite = toolkit.createComposite(section);
		composite.setLayout(new GridLayout(1, false));
		section.setClient(composite);
		gd = new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL);
		location = toolkit.createText(composite, "",
				SWT.BORDER);
		gd.widthHint = 100;
		location.setLayoutData(gd);
		location.addModifyListener(getTextModifyListener());
		
		/*
		 * Description
		 */
		section = toolkit.createSection(form.getBody(), Section.TITLE_BAR);
		td = new TableWrapData(TableWrapData.FILL_GRAB);
		section.setLayoutData(td);
		section.setText("Описание");
		composite = toolkit.createComposite(section);
		composite.setLayout(new GridLayout(1, false));
		section.setClient(composite);
		gd = new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL);
		description = toolkit.createText(composite, "",
				SWT.BORDER | SWT.MULTI | SWT.WRAP | SWT.V_SCROLL);
		gd.widthHint = 200;
		gd.heightHint = 60;
		description.setLayoutData(gd);
		description.addModifyListener(getTextModifyListener());
		
		/*
		 * Values
		 */
		section = toolkit.createSection(form.getBody(), Section.TITLE_BAR
				| Section.TWISTIE | Section.EXPANDED);
		td = new TableWrapData(TableWrapData.FILL);
		td.heightHint = 300;
		section.setLayoutData(td);
		section.setText("Список контролируемых технологических параметров");
		composite = toolkit.createComposite(section);
		section.setClient(composite);
		valuesEditor = new CalanderTableEditor(composite);
		valuesEditor.setInput(new ArrayList<Entity>());
		valuesEditor.addModifyListener(getEntityTableListener());

	}

}
